package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.response.IndexBeanV3Response;
import cn.wit.summit.chwan.utils.IntentUtil;

/**
 * Created by Alan on 2018/7/19.
 */

public class HomeIndexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;

    public HomeIndexAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public enum ViewType {
        TYPE_LINE,
        TYPE_ITEM
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;
        float downLoadRard;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewBean(ViewType viewType, Object obj, float downLoadRard) {
            this.viewType = viewType;
            this.obj = obj;
            this.downLoadRard = downLoadRard;
        }

        public float getDownLoadRard() {
            return downLoadRard;
        }

        public void setDownLoadRard(float downLoadRard) {
            this.downLoadRard = downLoadRard;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_LINE.ordinal()) {
            holder = new ItemHolder(inflater.inflate(R.layout.item_11_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_ITEM.ordinal()) {
            holder = new ItemHolder(inflater.inflate(R.layout.item_home_v3_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (viewBean.getViewType().ordinal() == ViewType.TYPE_ITEM.ordinal()) {
            final GameListBaseBean temp = (GameListBaseBean) viewBean.getObj();
            ItemHolder itemHolder = (ItemHolder) holder;

            itemHolder.itemGameImgIv.setImageURI(temp.getGame_ico());
            itemHolder.itemGameNameTx.setText(temp.getGame_name());
            itemHolder.itemGameSizeTx.setText(temp.getGame_size() + "M");
            itemHolder.itemGameDescTx.setText(temp.getGame_info());

            DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(temp.getGame_id());
            IndexBeanV3Response.CreditInfoBean tempBean = temp.getCredit_info();
            if (tempBean!=null && tempBean.getDay_surplus()>0 && tempBean.getGame_down_load_reward()>0
                    && (taskTable==null || taskTable.getDownStatus()==0)) {
                itemHolder.itemGameRewardTv.setVisibility(View.VISIBLE);
                itemHolder.itemGameRewardTv.setText("+ " + tempBean.getGame_down_load_reward() + "元");
            } else {
                itemHolder.itemGameRewardTv.setVisibility(View.INVISIBLE);
            }

//            itemHolder.itemGameRewardTv.setText("+ "+ viewBean.getDownLoadRard() +"元");
//            itemHolder.itemGameDetaliBt.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    IntentUtil.getInstance().goGamedetial(mContext, temp.getGame_id(), 1);
//                }
//            });
            itemHolder.itemDownloadRootRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().goGamedetial(mContext, temp.getGame_id(), 1);
                }
            });
            if (position==mDatas.size()-1) {
                itemHolder.itemBottomV.setVisibility(View.VISIBLE);
            } else {
                itemHolder.itemBottomV.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemDownloadRootRl;
        SimpleDraweeView itemGameImgIv;
        TextView itemGameNameTx;
        TextView itemGameSizeTx;
        TextView itemGameDescTx;
        Button itemGameDetaliBt;
        TextView itemGameRewardTv;
        View itemBottomV;

        public ItemHolder(View view) {
            super(view);
            itemDownloadRootRl = view.findViewById(R.id.itemDownloadRootRl);
            itemGameImgIv = view.findViewById(R.id.itemGameImgIv);
            itemGameNameTx = view.findViewById(R.id.itemGameNameTx);
            itemGameSizeTx = view.findViewById(R.id.itemGameSizeTx);
            itemGameDescTx = view.findViewById(R.id.itemGameDescTx);
            itemGameDetaliBt = view.findViewById(R.id.itemGameDetaliBt);
            itemGameRewardTv = view.findViewById(R.id.itemGameRewardTv);
            itemBottomV = view.findViewById(R.id.itemBottomV);
        }
    }
}
